# Creating Line Chart for Figure 1 Observing Capitalist Peace
setwd("~/Dropbox/!!Papers/Liberal Peace/Nina Gartzke paper/2013-20/ReplicationFiles")

data <- read.csv("Output/11-07-14predicted.csv")
set.seed(1234)

# convert factor to numeric for convenience 
capopenl<-as.numeric(data$capopenl) 

#Replace NA with 0
data =replace(data, is.na(data), 0) 

# get the range for the x and y axis 
xrange <- range(capopenl) 
yrange <- range(data[,names(data) != "capopenl"]) 
yrange1 = c(0, 0.01)
yrange2 = c(0, 0.003)

#setting location for 95 CI symbol in legends
replace1=c(0.18,.9,.9,0.18)
replace2=c(0.0087,0.0087,0.0090,0.0090)
replace3=replace2/3.333333

pdf('Figures/Figure1.pdf', width=10, height=6)
#making two plots
par(mfrow=c(1,2))

# set up the plot 
plot(xrange, yrange1,  xlab="Lower Capital Openness", xaxs = "i", yaxs = "i",
     ylab="Probability of MID" ) 


#adding 95 CI
polygon(c(capopenl,rev(capopenl)), c(t(data[4]),rev(t(data[5]))), col=grey(0.9))
#polygon(rbind(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8), rbind(data[5,1],data[4,1],data[5,2],data[4,2],data[5,3],data[4,3],data[5,4],data[4,4],data[5,5],data[4,5],data[5,6],data[4,6],data[5,7],data[4,7],data[5,8],data[4,8]), col="gray")

#abline(v=0, h=0)
colors <- c(grey(0), grey(0),grey(0.9),grey(0.9)) 
linetype <- c(1,2,1,1)
plotchar <- c(6,7,8,9)

#polygon(xx, yy, col="gray")


# adding lines
for (i in 1:2) { 
  #tree <- subset(capeopenl, Tree==i) 
  lines(t(capopenl), t(data[i+1]), lwd=2,
    lty=linetype[i], col=colors[i], pch=plotchar[i]) 
} 


# add a title and subtitle 
#title("Predicted Probability of MID")

# add a legend 
legend(xrange[1], yrange1[2], c("Unconditional Association","Predicted Probability from GLB Model", "95% Confidence Interval"), cex=0.8, col=colors, bg="white", lty=linetype)#, title="Predicted Probability of MID")
       
polygon(replace1, replace2, col=grey(0.9))




# SET UP SECOND PLOT
plot(xrange, yrange2,  xlab="Lower Capital Openness", xaxs = "i", yaxs = "i",
     ylab="Probability of Fatal MID" ) 
#abline(v=0, h=0)
colors <- c(grey(0), grey(0),grey(0.9),grey(0.9)) 
linetype <- c(1,2,1,1)
plotchar <- c(6,7,8,9)

#polygon(xx, yy, col="gray")
polygon(c(capopenl,rev(capopenl)), c(t(data[8]),rev(t(data[9]))), col=grey(0.9))
#polygon(rbind(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8), rbind(data[5,1],data[4,1],data[5,2],data[4,2],data[5,3],data[4,3],data[5,4],data[4,4],data[5,5],data[4,5],data[5,6],data[4,6],data[5,7],data[4,7],data[5,8],data[4,8]), col="gray")


# add lines 
for (i in 5:6) { 
  #tree <- subset(capeopenl, Tree==i) 
  lines(t(capopenl), t(data[i+1]), lwd=2,
    lty=linetype[i-4], col=colors[i-4], pch=plotchar[i-4]) 
} 


# add a title and subtitle 
#title("Predicted Probability of Fatal MID")

# add a legend 
legend(xrange[1], yrange2[2], c("Unconditional Association","Predicted Probability from GLB Model", "95% Confidence Interval"), cex=0.8, col=colors,
lty=linetype)#, title="Predicted Probability of MID")
       
polygon(replace1, replace3, col=grey(0.9))

#par(mfrow=c(1,1))
dev.off()
